<template>
  <el-dialog
    title="发送邮件"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="模板ID" prop="templateId">
        <el-input v-model="dataForm.templateId" placeholder="模板ID"></el-input>
      </el-form-item>
      <el-form-item label="收件人" prop="mailTo">
        <el-input v-model="dataForm.mailTo" placeholder="收件人邮箱"></el-input>
      </el-form-item>
      <el-form-item label="抄送" prop="mailCc">
        <el-input v-model="dataForm.mailCc" placeholder="抄送，可为空"></el-input>
      </el-form-item>
      <el-form-item label="模板参数" prop="params">
        <el-input v-model="dataForm.params" placeholder='如：{"code": "123456"}'></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isEmail } from '@/utils/validate'
  export default {
    data () {
      var validateEmail = (rule, value, callback) => {
        if (!isEmail(value)) {
          callback(new Error('邮箱格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          templateId: '',
          mailTo: '',
          mailCc: '',
          params: ''
        },
        dataRule: {
          templateId: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],
          mailTo: [
            { required: true, message: '必填项不能为空', trigger: 'blur' },
            { validator: validateEmail, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http.post(
              `${window.SITE_CONFIG['baseURL']}/sys/mailtemplate/send`,
              {
                'templateId': this.dataForm.templateId,
                'mailTo': this.dataForm.mailTo,
                'mailCc': this.dataForm.mailCc,
                'params': this.dataForm.params
              },
              {
                headers: { 'content-type': 'application/x-www-form-urlencoded' }
              }
            ).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
